/* SPDX-License-Identifier: GPL-2.0 */

#ifndef __DIM_JSON_H
#define __DIM_JSON_H

/**
 * enum json_type - flag of json type
 */
enum dim_json_type {
	DIM_JSON_ARRAY,
	DIM_JSON_OBJECT,
	DIM_JSON_STRING,
	DIM_JSON_UNKNOWN,
};

/**
 * struct json_object - json object of unmarshal result
 * @type: type of this object
 * @key: key value of this object, NULL for root object
 * @value: value of this object
 */
struct dim_json_object {
/* private: internal use only */
	struct dim_json_object *next;
	enum dim_json_type type;
	char *key;
	void *value;
};

/**
 * typedef dim_json_iterator - iterator function of json array
 * @obj: json object in array
 * @data: data held during iteration
 *
 * Return: 0 on success or other values on failure
 */
typedef int (*dim_json_iterator)(struct dim_json_object *obj, void *data);

/**
 * dim_json_unmarshal() - Unmarshal parses the JSON-encoded data
 * @buf: a pointer to json text
 * @buf_len: the length of json data
 *
 * Returns: a pointer to json object or errno on failure (via ERR_PTR)
 */
struct dim_json_object *dim_json_unmarshal(char *buf, unsigned long buf_len);

/**
 * dim_json_free() - Free the json object created by dim_json_unmarshal()
 * @obj: a pointer to json object
 */
void dim_json_free(struct dim_json_object *obj);

/**
 * dim_json_get_object() - Search and return the sub json oject
 * @obj: json object to be searched, must be JSON_OBJECT type
 * @key: json key to be searched
 * @type: json type to be searched
 *
 * Returns: a pointer to searched json object or NULL on failure
 */
struct dim_json_object *dim_json_get_object(struct dim_json_object *obj,
					    const char *key,
					    enum dim_json_type type);

/**
 * dim_json_iterator_array() - Travse a json object in JSON_ARRAY type
 * @obj: json object to be searched, must be JSON_OBJECT type
 * @f: travse function
 * @data: data held during iteration
 *
 * Returns: 0 on success or errno on failure
 */
int dim_json_iterator_array(struct dim_json_object *obj,
			    dim_json_iterator f,
			    void *data);

#endif
